package com.sichen.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sichen.common.utils.PageUtils;
import com.sichen.common.utils.Query;
import com.sichen.product.dao.BrandDao;
import com.sichen.product.dao.CategoryBrandRelationDao;
import com.sichen.product.dao.CategoryDao;
import com.sichen.product.entity.BrandEntity;
import com.sichen.product.entity.CategoryBrandRelationEntity;
import com.sichen.product.entity.CategoryEntity;
import com.sichen.product.service.BrandService;
import com.sichen.product.service.CategoryBrandRelationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Resource
    BrandDao brandDao;

    @Resource
    CategoryDao categoryDao;

    @Resource
    CategoryBrandRelationDao categoryBrandRelationDao;

    @Resource
    BrandService brandService;


    /**
     * 查询商品品牌和分类的关联列表
     * @param params 分页的条件
     * @return 分页后的数据
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<>()
        );
        return new PageUtils(page);
    }


    /**
     * 保存分类和品牌的关联关系
     * @param categoryBrandRelation 分类和品牌的关联关系
     */
    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        //1.获取分类id , 和品牌id
        Long catelogId = categoryBrandRelation.getCatelogId();
        Long brandId = categoryBrandRelation.getBrandId();
        //2.查询品牌数据
        BrandEntity brandEntity = brandDao.selectById(brandId);
        //3.查询分类数据
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
        //4.保存品牌名称和分类名称到数据库
        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        this.save(categoryBrandRelation);
    }

    @Override
    public void updateBrand(Long brandId, String name) {
        CategoryBrandRelationEntity relationEntity = new CategoryBrandRelationEntity();
        relationEntity.setBrandId(brandId);
        relationEntity.setBrandName(name);
        this.update(relationEntity,new UpdateWrapper<CategoryBrandRelationEntity>()
                .eq("brand_id",brandId)
        );
    }

    @Override
    public void updateCategory(Long catId, String name) {
        CategoryBrandRelationEntity relationEntity = new CategoryBrandRelationEntity();
        relationEntity.setCatelogId(catId);
        relationEntity.setCatelogName(name);
        this.update(relationEntity,new UpdateWrapper<CategoryBrandRelationEntity>()
                .eq("catelog_id",catId)
        );
    }
}